package invoiceManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import javax.swing.JPanel;

import dbManage.LineItem;

public class Invoice extends JPanel implements Serializable{
	
	public static final int ESUPPLY = 1;
	public static final int GOLDMARK = 2;
	public static final int NONE = 0;
	
	private boolean sendElectronically;
	private Date invoiceDate;
	private String customerID, shipLineOne, shipLineTwo, shipLineThree, shipLineFour, billLineOne, billLineTwo, billLineThree, billLineFour;
	private String phoneNumber, dateShipped, dateOrdered, salesNumber, salesName, po;
	private char pricing;
	private String managementCo;
	private String invoiceNumber;
	private int cutNumber;
	private String adblock;
	private double nonTaxable, taxable, tax, handling, labor, total;
	private ArrayList<LineItem> lineItems;
	private int electronicHandler;
	
	public Invoice(String inputStream){
		Scanner scan = new Scanner(inputStream);
		invoiceDate = new Date();
		lineItems = new ArrayList<LineItem>();
		int lineNumber = 1;
		
		while(scan.hasNextLine()){
			String data = scan.nextLine();
			switch(lineNumber){
				case 3:
					shipLineOne = data.substring(10).trim();
					break;
				case 4:
					customerID = data.substring(40,46);
					data.replace(customerID,"");
					shipLineTwo = data.substring(10).trim();
					break;
				case 5:
					if(data.length()>90){
						shipLineThree = data.substring(10,41).trim();
					}
					else{
						shipLineThree = data.substring(10);
					}
					break;
				case 6:
					if(data.contains("$")){
						shipLineFour = data.substring(10).trim();
						billLineOne = scan.nextLine().substring(10).trim();
					}
					else{
						shipLineFour = "";
						billLineOne = data.substring(10).trim();
					}
					break;
				case 7:
					billLineTwo = data.substring(10).trim();
					break;
				case 8:
					billLineThree = data.substring(10).trim();
					break;
				case 9:
					if(data.contains("%")){
						billLineFour = data.substring(10).trim();
						phoneNumber = scan.nextLine().trim();
					}
					else{
						billLineFour = "";
						phoneNumber = data.trim();
					}
					break;
				case 11:
					salesName = data.trim();
					break;
				case 12:
					dateShipped = data.substring(1,11).trim();
					salesNumber = data.substring(15,19).trim();
					pricing = data.charAt(19);
					dateOrdered = data.substring(32,42).trim();
					managementCo = data.substring(42,64).trim();
					invoiceNumber = data.substring(75).trim();
					break;
				case 13:
					while(!data.contains("*******************************************************")){
						LineItem li = new LineItem(data, LineItem.INVOICE);
						data = scan.nextLine();
					}
					adblock = data.trim();
					break;
				case 14:
					adblock =adblock+  "\n" + data.trim();
					if(data.contains("S E N D    E L E C T R O N I C A L L Y")){
						sendElectronically = true;
					}
					break;
				case 15:
					adblock = adblock + "\n" + data.trim();
					break;
				case 16:
					adblock = adblock + "\n" + data.trim();
					break;
				case 17:
					adblock = adblock + "\n" + data.trim();
					break;
				case 18:
					adblock = adblock + "\n" + data.trim();
					break;
				case 20:
					try{
						cutNumber = Integer.parseInt(data.substring(1,8).trim());
						nonTaxable = Double.parseDouble(data.substring(8,22).trim());
						taxable = Double.parseDouble(data.substring(22,34).trim());
						tax = Double.parseDouble(data.substring(34,44).trim());
						handling = Double.parseDouble(data.substring(44,54).trim());
						labor = Double.parseDouble(data.substring(54,64).trim());
						total = Double.parseDouble(data.substring(64).trim());
					}
					catch(Exception e){}
					break;				
			}
			lineNumber++;
		}
		scan.close();
	}

	public Date getInvoiceDate() {
		return invoiceDate;
	}

	public void setInvoiceDate(Date invoiceDate) {
		this.invoiceDate = invoiceDate;
	}

	public String getCustomerID() {
		return customerID;
	}

	public void setCustomerID(String customerID) {
		this.customerID = customerID;
	}

	public String getInvoiceNumber() {
		return invoiceNumber;
	}

	public void setInvoiceNumber(String invoiceNumber) {
		this.invoiceNumber = invoiceNumber;
	}

	public int getCutNumber() {
		return cutNumber;
	}

	public void setCutNumber(int cutNumber) {
		this.cutNumber = cutNumber;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}

	public String getBillLineOne() {
		return billLineOne;
	}

	public void setBillLineOne(String billLineOne) {
		this.billLineOne = billLineOne;
	}

	public int getElectronicHandler() {
		return electronicHandler;
	}

	public void setElectronicHandler(int electronicHandler) {
		this.electronicHandler = electronicHandler;
	}
}
